package com.pzhu.acargid.paper;

import com.alibaba.fastjson.JSON;
import com.pzhu.acargid.coauthor.CoauthorUtil;
import com.pzhu.acargid.doc.DocService;
import com.pzhu.acargid.domain.Coauthor;
import com.pzhu.acargid.domain.Doc;
import com.pzhu.acargid.domain.Paper;
import com.pzhu.acargid.until.Result;
import com.pzhu.acargid.vo.PaperVO;
import com.pzhu.file.FileController;
import com.pzhu.file.PdfImage;
import com.pzhu.file.utils.FileUtil;
import com.pzhu.security.token.JwtTokenService;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/paper")
public class PaperController {
    @Autowired
    private PaperService paperService;
    @Autowired
    private DocService docService;
    @Autowired
    private CoauthorUtil coauthorUtil;

    private final Logger logger = LoggerFactory.getLogger(PaperController.class);

//    @PostMapping("/editweight")
//    public Result editWeight(HttpSession session, Integer modelId, Integer docId, Integer weight) {
//        Integer userid = (Integer) session.getAttribute("userid");
//        LambdaUpdateWrapper<Paper> paperUpdateWrapper = new LambdaUpdateWrapper<>();
//        paperUpdateWrapper.eq(Paper::getDocId, docId);
//        paperUpdateWrapper.eq(Paper::getUserId, userid);
//        paperUpdateWrapper.set(Paper::getWeight, weight);
//        if (paperService.update(paperUpdateWrapper))
//            return new Result(1, null, "操作成功");
//        return new Result(0, null, "操作失败");
//    }

    @PostMapping
    public Result upload(HttpServletRequest request, @RequestParam("data") String data, @RequestParam("images") String images) throws IOException {
        Integer userid = JwtTokenService.getClaim(request, "userid").asInt();
        PaperVO paperVO = JSON.parseObject(data, PaperVO.class);
        List<PdfImage> list = JSON.parseArray(images, PdfImage.class);
        List<String> collect = list.stream().map(PdfImage::getImage).collect(Collectors.toList());
        List<Coauthor> coauthors = paperVO.getCoauthors();
        paperVO.setUserId(userid);
        paperVO.setRemoved(0);
        paperVO.setModType("paper");
        paperVO.setModDbTable("paper");
        Doc doc = new Doc();
        Paper paper = new Paper();
        BeanUtils.copyProperties(paperVO, doc);
        BeanUtils.copyProperties(paperVO, paper);
        doc.setId(null);
        paper.setDocId(null);
        docService.save(doc);
        paper.setDocId(doc.getId());
        paperService.save(paper);
        String savePath = FileUtil.getSavePath(userid, "paper") + doc.getId();
        FileUtil.makeDir(savePath, logger);
        FileController.savePDF(paperVO.getTitle(), savePath + File.separator + paperVO.getTitle() + ".pdf", collect);
        return coauthorUtil.getResult(coauthors, doc, userid);
    }

    @PutMapping
    public Result update(HttpServletRequest request, @RequestParam("data") String data, @RequestParam("images") String images) throws IOException {
        Integer userid = JwtTokenService.getClaim(request, "userid").asInt();
        PaperVO paperVO = JSON.parseObject(data, PaperVO.class);
        List<PdfImage> list = JSON.parseArray(images, PdfImage.class);
        List<String> collect = list.stream().map(PdfImage::getImage).collect(Collectors.toList());
        String savePath = FileUtil.getSavePath(userid, "paper") + paperVO.getId();
        FileUtil.makeDir(savePath, logger);
        FileUtils.cleanDirectory(new File(savePath));
        FileController.savePDF(paperVO.getTitle(), savePath + File.separator + paperVO.getTitle() + ".pdf", collect);
        List<Coauthor> coauthors = paperVO.getCoauthors();
        paperVO.setUserId(userid);
        paperVO.setRemoved(0);
        paperVO.setModType("paper");
        paperVO.setModDbTable("paper");
        paperVO.setUserId(userid);
        Doc doc = new Doc();
        Paper paper = new Paper();
        BeanUtils.copyProperties(paperVO, doc);
        BeanUtils.copyProperties(paperVO, paper);
        docService.updateById(doc);
        paperService.updateById(paper);
        return coauthorUtil.update(coauthors, doc, userid);
    }
}
